﻿Imports System.IO
Imports MessageBoxEx
Imports System.Collections.Specialized

Public Class frmSettings
    Private _changes As Boolean = False

    Private Sub buttonLocateDemonbuddy_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles buttonLocateDemonbuddy.Click

        Dim dialog As String

        dialog = Dialog_BrowseDirectory()

        If dialog = Nothing Then Exit Sub

        If (Directory.GetFiles(dialog, "Demonbuddy.exe", SearchOption.TopDirectoryOnly).Count > 0) Then

            Dim _Msg =
                    New MsgBoxEx(
                        "The Demonbuddy executable (Demonbuddy.exe) could not be found in this directory, would you like to download it to this directory?",
                        Me.Text, , MsgBoxExImage.Question)
            _Msg.SetButtons(New String() {"Yes", "No"},
                            New System.Windows.Forms.DialogResult() _
                               {System.Windows.Forms.DialogResult.Yes, DialogResult.No}, 1)
            _Msg.ShowDialog(Me)

            If _Msg.DialogBoxResult = DialogBoxResult.Button1 Then

                textboxFolderDemonbuddy.Text = dialog
                buttonDownloadDemonbuddy.PerformClick()
            Else
                Exit Sub

            End If

        Else

            labelDemonbuddyVersion.Text = File_GetVersionInfo(Path.Combine(dialog.ToString, "Demonbuddy.exe"))
            textboxFolderDemonbuddy.Text = dialog

        End If
    End Sub

    Private Sub buttonLocateUtilities_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles buttonLocateUtilities.Click

        Dim dialog As System.Windows.Forms.DialogResult

        dialog = Dialog_BrowseDirectory()

        If dialog = Nothing Then Exit Sub

        If String_IsEmpty(dialog.ToString) = True Then

            dialog = frmMain.defaultPatch

        End If

        textboxFolderPatch.Text = dialog.ToString
    End Sub

    Private Sub frmStartup_AppSettings_Read(ByVal sender As Object, ByVal e As EventArgs) Handles Me.Load

        frmMain.Enabled = False

        Startup_AppSettings_Read()
    End Sub

    Private Sub cmdModFolderAdd_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonModAdd.Click

        Dim dialog As String

        dialog = Dialog_BrowseDirectory()

        If String_IsEmpty(dialog) Then Exit Sub

        listboxModLocations.Items.Add(dialog)

        _changes = True
    End Sub

    Private Sub cmdModFolderRem_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonPatchRemove.Click

        Dim i As Integer

        i = listboxModLocations.SelectedIndex

        If i > -1 Then
            listboxModLocations.Items.Remove(listboxModLocations.SelectedItem)
        End If

        _changes = True
    End Sub

    Private Sub cmdSave_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonSaveSettings.Click

        Settings_Save()

        frmMain.Enabled = True

        If _changes = True Then

            frmMain.Startup_AppSettings_Read()

            frmMain._triggerReadMods = True

            frmMain.timerProcessingText.Start()

        End If

        Hide()
    End Sub

    Private Sub Settings_Save()

        Dim dirModifications(listboxModLocations.Items.Count - 1) As String

        listboxModLocations.Items.CopyTo(dirModifications, 0)

        My.Settings.FolderMods = New StringCollection
        My.Settings.FolderMods.AddRange(dirModifications)

        If (String_IsEmpty(textboxFolderDemonbuddy.Text) = False) And
           (File.Exists(Path.Combine(textboxFolderDemonbuddy.Text, "Demonbuddy.exe")) = True) Then

            If Strings.Right(textboxFolderDemonbuddy.Text, 1) = "\" Then
                My.Settings.FolderDemonbuddy = Path.GetDirectoryName(textboxFolderDemonbuddy.Text)
            Else
                My.Settings.FolderDemonbuddy = textboxFolderDemonbuddy.Text
            End If

        End If

        If (String_IsEmpty(textboxFolderDiablo.Text) = False) And
           (File.Exists(Path.Combine(textboxFolderDiablo.Text, "Diablo III.exe")) = True) Then

            If Strings.Right(textboxFolderDiablo.Text, 1) = "\" Then
                My.Settings.FolderDiablo = Path.GetDirectoryName(textboxFolderDiablo.Text)
            Else
                My.Settings.FolderDiablo = textboxFolderDiablo.Text
            End If

        End If

        If Directory.Exists(textboxFolderProfile.Text) = False Then

            Directory_Check(textboxFolderProfile.Text)

            If Strings.Right(textboxFolderProfile.Text, 1) = "\" Then
                My.Settings.FolderProfile = Path.GetDirectoryName(textboxFolderProfile.Text)
            Else
                My.Settings.FolderProfile = textboxFolderProfile.Text
            End If

        End If

        If Directory.Exists(textboxFolderItemRules.Text) = False Then

            Directory_Check(textboxFolderItemRules.Text)

            If Strings.Right(textboxFolderItemRules.Text, 1) = "\" Then
                My.Settings.FolderItemRule = Path.GetDirectoryName(textboxFolderItemRules.Text)
            Else
                My.Settings.FolderItemRule = textboxFolderItemRules.Text
            End If

        End If

        If Directory.Exists(textboxFolderBackup.Text) = False Then

            Directory_Check(textboxFolderBackup.Text)

            If Strings.Right(textboxFolderBackup.Text, 1) = "\" Then
                My.Settings.FolderBackup = Path.GetDirectoryName(textboxFolderBackup.Text)
            Else
                My.Settings.FolderBackup = textboxFolderBackup.Text
            End If

        End If

        If Directory.Exists(textboxFolderPatch.Text) = False Then

            Directory_Check(textboxFolderPatch.Text)

            If Strings.Right(textboxFolderPatch.Text, 1) = "\" Then
                My.Settings.FolderPatch = Path.GetDirectoryName(textboxFolderPatch.Text)
            Else
                My.Settings.FolderPatch = textboxFolderPatch.Text
            End If

        End If

        My.Settings.Save()
    End Sub

    Private Sub Startup_AppSettings_Read()

        Dim dirDemonbuddy As String = My.Settings.FolderDemonbuddy
        Dim dirBackup As String = My.Settings.FolderBackup
        Dim dirPatch As String = My.Settings.FolderPatch
        Dim dirItemRule As String = My.Settings.FolderItemRule
        Dim dirProfile As String = My.Settings.FolderProfile
        Dim dirDiablo As String = My.Settings.FolderDiablo

        Dim dirModifications(My.Settings.FolderMods.Count - 1) As String

        My.Settings.FolderMods.CopyTo(dirModifications, 0)
        listboxModLocations.Items.Clear()
        listboxModLocations.Items.AddRange(dirModifications)

        textboxFolderBackup.Text = dirBackup
        textboxFolderDemonbuddy.Text = dirDemonbuddy
        textboxFolderDiablo.Text = dirDiablo
        textboxFolderProfile.Text = dirProfile
        textboxFolderItemRules.Text = dirItemRule
        textboxFolderPatch.Text = dirPatch
        labelDemonbuddyVersion.Text = File_GetVersionInfo(Path.Combine(dirDemonbuddy, "Demonbuddy.exe")).ToString
    End Sub

    Private Sub buttonLocateDiablo_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles buttonLocateDiablo.Click

        Dim dialog As System.Windows.Forms.DialogResult

        dialog = Dialog_BrowseDirectory()

        If dialog = Nothing Then Exit Sub

        If String_IsEmpty(dialog.ToString) Then Exit Sub

        dialog = dialog.ToString

        If File.Exists(Path.Combine(dialog.ToString, "\Diablo III.exe")) = False Then

            Dim _Msg =
                    New MsgBoxEx(
                        "The Diablo III executable (Diablo III.exe) could not be found in this directory, please try again.",
                        Me.Text, , MsgBoxExImage.Exclamation)
            _Msg.SetButtons(New String() {"OK"},
                            New System.Windows.Forms.DialogResult() {System.Windows.Forms.DialogResult.Yes}, 1)
            _Msg.ShowDialog(Me)

        Else

            textboxFolderDiablo.Text = dialog.ToString

        End If
    End Sub

    Private Sub buttonDownloadDemonbuddy_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles buttonDownloadDemonbuddy.Click

        Dim dirDemonbuddy As String = textboxFolderDemonbuddy.Text
        Dim _result As Boolean = Download_DemonbuddyExe(dirDemonbuddy, progDemonbuddy)

        If _result = True Then

            labelDemonbuddyVersion.Text = File_GetVersionInfo(Path.Combine(dirDemonbuddy, "Demonbuddy.exe"))
            Dim _Msg = New MsgBoxEx("Download was successful.", Me.Text, , MsgBoxExImage.Check)
            _Msg.SetButtons(New String() {"OK"},
                            New System.Windows.Forms.DialogResult() {System.Windows.Forms.DialogResult.Yes}, 1)
            _Msg.ShowDialog(Me)

        End If
    End Sub

    Private Sub buttonDownloadPatch_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles buttonDownloadPatch.Click

        Dim dirPatch As String = textboxFolderPatch.Text
        Dim _result As Boolean = Download_PatchExe(dirPatch, progPatch)

        If _result Then

            Dim _Msg = New MsgBoxEx("Download was successful.", Me.Text, , MsgBoxExImage.Check)
            _Msg.SetButtons(New String() {"OK"},
                            New System.Windows.Forms.DialogResult() {System.Windows.Forms.DialogResult.Yes}, 1)
            _Msg.ShowDialog(Me)

        End If
    End Sub

    Private Sub buttonLocateProfile_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles buttonLocateProfile.Click

        Dim dialog As String

        dialog = Dialog_BrowseDirectory()

        If dialog = Nothing Then Exit Sub

        If String_IsEmpty(dialog.ToString) Then Exit Sub

        textboxFolderProfile.Text = dialog.ToString
    End Sub

    Private Sub cmdCancel_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonCancelSettings.Click

        Startup_AppSettings_Read()

        frmMain.Enabled = True

        Hide()
    End Sub

    Private Sub buttonLocateItemRules_Click(ByVal sender As Object, ByVal e As EventArgs) _
        Handles buttonLocateItemRules.Click

        Dim dialog As String

        dialog = Dialog_BrowseDirectory()

        If dialog = Nothing Then Exit Sub

        If String_IsEmpty(dialog.ToString) Then Exit Sub

        textboxFolderItemRules.Text = dialog.ToString
    End Sub

    Private Sub buttonLocateBackup_Click(ByVal sender As Object, ByVal e As EventArgs) Handles buttonLocateBackup.Click

        Dim dialog As String

        dialog = Dialog_BrowseDirectory()

        If dialog = Nothing Then Exit Sub

        If String_IsEmpty(dialog.ToString) Then Exit Sub

        textboxFolderBackup.Text = dialog.ToString
    End Sub

    Private Sub textboxFolderDemonbuddy_LostFocus(ByVal sender As Object, ByVal e As EventArgs) _
        Handles textboxFolderDemonbuddy.LostFocus

        Dim obj As TextBox = DirectCast(sender, TextBox)

        If String_IsEmpty(obj.Text) Then

            Dim _Msg =
                    New MsgBoxEx(
                        "The directory you have specified does not exists. Would you like to set it to default?",
                        Me.Text, , MsgBoxExImage.Question)
            _Msg.SetButtons(New String() {"Yes", "No"},
                            New System.Windows.Forms.DialogResult() _
                               {System.Windows.Forms.DialogResult.Yes, DialogResult.No}, 2)
            _Msg.ShowDialog(Me)

            If _Msg.DialogBoxResult = DialogBoxResult.Button1 Then

                If Directory.Exists(frmMain.defaultDemonbuddy) = False Then

                    Directory.CreateDirectory(frmMain.defaultDemonbuddy)

                End If

                obj.Text = frmMain.defaultDemonbuddy

            End If

        End If
    End Sub

    Private Sub textboxFolderBackup_LostFocus(ByVal sender As Object, ByVal e As EventArgs) _
        Handles textboxFolderBackup.LostFocus

        Dim obj As TextBox = DirectCast(sender, TextBox)

        If String_IsEmpty(obj.Text) Then

            obj.Text = frmMain.defaultBackup

        End If
    End Sub

    Private Sub textboxFolderItemRules_LostFocus(ByVal sender As Object, ByVal e As EventArgs) _
        Handles textboxFolderItemRules.LostFocus

        Dim obj As TextBox = DirectCast(sender, TextBox)

        If String_IsEmpty(obj.Text) Then

            obj.Text = frmMain.defaultItemRules

        End If
    End Sub

    Private Sub textboxFolderPatch_LostFocus(ByVal sender As Object, ByVal e As EventArgs) _
        Handles textboxFolderPatch.LostFocus

        Dim obj As TextBox = DirectCast(sender, TextBox)

        If String_IsEmpty(obj.Text) Then

            Dim _Msg =
                    New MsgBoxEx(
                        "The directory you have specified does not exists. Would you like to set it to default?",
                        Me.Text, , MsgBoxExImage.Question)
            _Msg.SetButtons(New String() {"Yes", "No"},
                            New System.Windows.Forms.DialogResult() _
                               {System.Windows.Forms.DialogResult.Yes, DialogResult.No}, 1)
            _Msg.ShowDialog(Me)

            If _Msg.DialogBoxResult = DialogBoxResult.Button1 Then

                If Directory.Exists(frmMain.defaultPatch) = False Then

                    Directory.CreateDirectory(frmMain.defaultPatch)

                End If

                obj.Text = frmMain.defaultPatch

                buttonDownloadPatch.PerformClick()

            End If

        End If
    End Sub

    Private Sub textboxFolderDiablo_LostFocus(ByVal sender As Object, ByVal e As EventArgs) _
        Handles textboxFolderDiablo.LostFocus

        Dim obj As TextBox = DirectCast(sender, TextBox)

        If String_IsEmpty(obj.Text) Then

            obj.Text =
                My.Computer.Registry.GetValue(
                    "HKEY_LOCAL_MACHINE\SOFTWARE\Wow6432Node\Microsoft\Windows\CurrentVersion\Uninstall\Diablo III",
                    "DisplayIcon", Nothing)

        End If
    End Sub

    Private Sub textboxFolderProfile_LostFocus(ByVal sender As Object, ByVal e As EventArgs) _
        Handles textboxFolderProfile.LostFocus

        Dim obj As TextBox = DirectCast(sender, TextBox)

        If String_IsEmpty(obj.Text) Then

            obj.Text = frmMain.defaultProfiles

        End If
    End Sub

    Private Sub textboxFolderDemonbuddy_TextChanged(sender As Object, e As EventArgs) _
        Handles textboxFolderDemonbuddy.TextChanged

        _changes = True
    End Sub

    Private Sub textboxFolderProfile_TextChanged(sender As Object, e As EventArgs) _
        Handles textboxFolderProfile.TextChanged

        _changes = True
    End Sub

    Private Sub textboxFolderItemRules_TextChanged(sender As Object, e As EventArgs) _
        Handles textboxFolderItemRules.TextChanged

        _changes = True
    End Sub

    Private Sub textboxFolderPatch_TextChanged(sender As Object, e As EventArgs) Handles textboxFolderPatch.TextChanged

        _changes = True
    End Sub

    Private Sub textboxFolderDiablo_TextChanged(sender As Object, e As EventArgs) _
        Handles textboxFolderDiablo.TextChanged

        _changes = True
    End Sub
End Class